package boundarys;

import java.awt.BorderLayout;
import java.awt.List;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Collections;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

import controllers.CustomerCharacterizationReportController;
import entitys.Reply;
import enums.Command;

public class CustomerCharacterazationReportGUI extends AbstractGUI {

	private JFrame frmCcrmcustomerCharacterazationReport;
	private JPanel reportPanel;
	private JPanel reportbtnPanel;
	private JPanel exportbtnPanel;
	private List reportList=null;
	private JButton showReportBtn;
	private JButton exportToTxtBtn;
	
	/**
	 * Add client GUI constructor. 
	 * This method initializes the window,buttons, labels etc...
	 */
	public CustomerCharacterazationReportGUI() {
		initialize();
		initGUI(frmCcrmcustomerCharacterazationReport);
		frmCcrmcustomerCharacterazationReport.getContentPane().setLayout(null);
		
		reportPanel = new JPanel();
		reportPanel.setBounds(0, 44, 444, 228);
		frmCcrmcustomerCharacterazationReport.getContentPane().add(reportPanel);
		reportPanel.setLayout(new BorderLayout(0, 0));
		
		reportList = new List();
		reportPanel.add(reportList, BorderLayout.CENTER);
		
		reportbtnPanel = new JPanel();
		reportbtnPanel.setBounds(0, 0, 224, 44);
		frmCcrmcustomerCharacterazationReport.getContentPane().add(reportbtnPanel);
		reportbtnPanel.setLayout(new BorderLayout(0, 0));
		
		showReportBtn = new JButton("Show Report");
		showReportBtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				CustomerCharacterizationReportController.getCustomerCharacterizationReport();
			}
		});
		reportbtnPanel.add(showReportBtn, BorderLayout.CENTER);
		
		exportbtnPanel = new JPanel();
		exportbtnPanel.setBounds(224, 0, 220, 44);
		frmCcrmcustomerCharacterazationReport.getContentPane().add(exportbtnPanel);
		exportbtnPanel.setLayout(new BorderLayout(0, 0));
		
		exportToTxtBtn = new JButton("Export To .txt");
		exportToTxtBtn.setEnabled(false);
		exportToTxtBtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
			    PrintWriter pw = null;
				try {
					pw = new PrintWriter(new OutputStreamWriter(new FileOutputStream("Customer Characterazation Report.txt"), "UTF-8"));
				} catch (UnsupportedEncodingException | FileNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			    try {
			        int len = reportList.getItemCount();
			        for (int i = 0; i < len; i++) {
			            pw.println(reportList.getItem(i).toString());
			        }
			    } finally {
			        pw.close();
			    }
			}
		});
		exportbtnPanel.add(exportToTxtBtn, BorderLayout.CENTER);
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frmCcrmcustomerCharacterazationReport = new JFrame();
		frmCcrmcustomerCharacterazationReport.setTitle("...Produce Report>Customer Characterazation Report");
		frmCcrmcustomerCharacterazationReport.setResizable(false);
		frmCcrmcustomerCharacterazationReport.setBounds(100, 100, 450, 300);
		frmCcrmcustomerCharacterazationReport.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		frmCcrmcustomerCharacterazationReport.setVisible(true);
	}

	@Override
	/**
	 * AbstractGUI overridden method for getting a reply from server 
	 */
	public void getReply(Object r) {
		
		if(((Reply)r).command.equals(Command.CUSTOMER_CHARACTERIZATION_REPORT))
		{
			ArrayList<String> s = (ArrayList<String>) ((Reply)r).result;
			for(int i=0;i<s.size();i++)
				for(int j=i;j<s.size();j++)
				{
					if(Integer.parseInt(s.get(i).substring(s.get(i).indexOf(" ")+1))<Integer.parseInt(s.get(j).substring(s.get(j).indexOf(" ")+1)))
					{
						Collections.swap(s, i, j);
					}
				}
			reportList.removeAll();
			for(String str:s)
				reportList.add(str);
			exportToTxtBtn.setEnabled(true);
		}
	}
}
